package com.zxbit.flash.ui.activity.mine;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.zxbit.flash.R;
import com.zxbit.flash.base.BaseTitleActivity;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.common.RouterMap;
import com.zxbit.flash.mvpview.BankCardMvpView;
import com.zxbit.flash.presenter.BankCardPresenter;
import com.zxbit.flash.response.BankListResponse;
import com.zxbit.flash.bean.BindedCardData;
import com.zxbit.flash.response.BindedCardResponse;
import com.zxbit.flash.response.BlankResponse;
import com.zxbit.flash.response.PersonalInfoResponse;
import com.zxbit.flash.utils.StringUtils;
import com.zxbit.flash.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的银行卡
 */
@Route(name = "我的银行卡", path = RouterMap.MINE_BANK_CARD)
public class MineBankCardActivity extends BaseTitleActivity implements BankCardMvpView {

    @BindView(R.id.tv_bank_card_name)
    TextView tvBankCardName;
    @BindView(R.id.tv_bank_card_user_name)
    TextView tvBankCardUserName;
    @BindView(R.id.tv_bank_card_num)
    TextView tvBankCardNum;
    @BindView(R.id.ll_bank_card)
    LinearLayout llBankCard;
//    @BindView(R.id.tv_change_bank_card)
//    TextView tvChangeBankCard; // 重新绑定银行卡

//    @BindView(R.id.ll_error)
//    RelativeLayout llError;
    @BindView(R.id.ll_no_data)
    RelativeLayout llNoData;

    private BankCardPresenter presenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        presenter = new BankCardPresenter(this);
        presenter.getBankInfo();
        showLoadingView();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_mine_bank_card;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.mine_bank_card);
    }

//    @OnClick(R.id.tv_change_bank_card)
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.tv_change_bank_card:
//                ARouter.getInstance().build(RouterMap.BANK_AUTH_PAGE).withBoolean("is_change", true)
//                        .navigation();
//                break;
//        }
//    }

    @Override
    public void onGetDataNext(BindedCardResponse bindedCardResponse) {
        dismissLoadingView();
        if (bindedCardResponse.getStatus() == Constant.HTTP_OK) {
            if (bindedCardResponse.hasData()) {
                fillCardView(bindedCardResponse.getData());
            } else {
                llNoData.setVisibility(View.VISIBLE);
                llBankCard.setVisibility(View.GONE);
//                llError.setVisibility(View.GONE);
//                tvChangeBankCard.setVisibility(View.GONE);
            }
        } else {
            llNoData.setVisibility(View.VISIBLE);
            llBankCard.setVisibility(View.GONE);
            ToastUtils.showShort(this, bindedCardResponse.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }

    private void fillCardView(BindedCardData data) {
//        if ("1".equals(data.getBankStatus())) {
//            llNoData.setVisibility(View.GONE);
//            llBankCard.setVisibility(View.VISIBLE);
//            tvTips.setVisibility(View.VISIBLE);
//            llError.setVisibility(View.VISIBLE);
//            tvChangeBankCard.setVisibility(View.VISIBLE);
//        } else {
//            llError.setVisibility(View.GONE);
//            tvChangeBankCard.setVisibility(View.GONE);
            String bank_card = data.getBankCard();
            if (StringUtils.isEmpty(bank_card)) {
                llNoData.setVisibility(View.VISIBLE);
                llBankCard.setVisibility(View.GONE);
            } else {
                llNoData.setVisibility(View.GONE);
                llBankCard.setVisibility(View.VISIBLE);
            }
//        }

        if (tvBankCardName != null) {
            tvBankCardName.setText(data.getBankCode());
        }
        if (tvBankCardNum != null) {
            if (StringUtils.isEmpty(bank_card)) return;
            String formatCardNum = bank_card;
            //格式化规则，银行卡 前后四位数字；中间使用*号代替
            if (bank_card.length() > 8) {
                String preFourNum = bank_card.substring(0, 4);
                String suffixFourNum = bank_card.substring(bank_card.length() - 4, bank_card
                        .length());

                formatCardNum = new StringBuilder(preFourNum).append(" ").append("****").append("" +
                        " ").append("****").append(" ").append(suffixFourNum).toString();
            }
            tvBankCardNum.setText(formatCardNum);
        }
        if (tvBankCardUserName != null) {
            tvBankCardUserName.setText(data.getRealName());
        }
    }

    @Override
    public void onGetDataNext(BlankResponse response) {}

    @Override
    public void onGetDataNext(PersonalInfoResponse response) {}

    @Override
    public void onGetDataNext(BankListResponse response) {}

}
